const mysql = require('mysql')

const connect = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'blog'
})

connect.connect()

const execSQL = (sql) => {
  const promise = new Promise((resovle, reject) => {
    connect.query(sql, (err, result) => {
      if (err) {
        reject(err);
        return;
      }
      if (result.length < 1) {
        resovle({
          code: -1,
          message: '查询失败'
        })
        return;
      }
      resovle({
        code: 0,
        data: result
      })
    })
  })
  return promise;
}

module.exports = execSQL;